package com.whq.thrift.api.web.admin.controller.bs;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.requestlog.RequestLog;
import com.whq.thrift.api.web.admin.service.requestlog.RequestLogService;
import com.whq.thrift.api.web.admin.util.DateFormatUtil;

@Controller
@Scope("prototype")
public class RequestLogStatController {
	@Autowired
	RequestLogService requestLogService;
	
	@RequestMapping(value="bs/requestlogstat/statByMethodCode")
	public String statByMethodCode(
	        @RequestParam(value="time", required=false) String time,
	        @RequestParam(value="start_time", required=false) String start_time,
	        @RequestParam(value="end_time", required=false) String end_time,
	        @RequestParam(value="method", required=false) String method,
	        @RequestParam(value="verify_code", required=false) Integer verify_code,
			Model model) {
		RequestLog requestLog = new RequestLog();
		
	    // 查询参数
	    Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    Date timeDt = (time != null && StringUtil.isNotBlank(time)) ? DateFormatUtil.formatToDate(time) :  new Date();
	    String timeDate = DateFormatUtil.dateToString(timeDt);
	    
	    requestLog.setTime(timeDt);
	    whereParams.put("tableSeq", requestLog.getTableSeq());
	    
	    if (StringUtil.isNotBlank(start_time)) {
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
	    	start_time = sdfHour.format(new Date()) +  ":00";
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    }
	    
	    if (StringUtil.isNotBlank(end_time)) {
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
	    	end_time = sdfTime.format(new Date());
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    }
	    
	    if (StringUtil.isNotBlank(method)) {
	    	whereParams.put("method", method);
	    }
	    
	    if (verify_code != null) {
	    	whereParams.put("verify_code", verify_code);
	    }
	    
	    List<RequestLog> dataList = requestLogService.statByMethodCode(whereParams);
	    
	    model.addAttribute("time", time);
	    model.addAttribute("start_time", start_time);
	    model.addAttribute("end_time", end_time);
	    model.addAttribute("method", method);
	    model.addAttribute("verify_code", verify_code);
		model.addAttribute("dataList", dataList);
		return "bs/requestlogstat/statByMethodCode";
	}
	
	@RequestMapping(value="bs/requestlogstat/statByCode")
	public String statByCode(
	        @RequestParam(value="time", required=false) String time,
	        @RequestParam(value="start_time", required=false) String start_time,
	        @RequestParam(value="end_time", required=false) String end_time,
	        @RequestParam(value="verify_code", required=false) Integer verify_code,
			Model model) {
		RequestLog requestLog = new RequestLog();
		
	    // 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    Date timeDt = (time != null && StringUtil.isNotBlank(time)) ? DateFormatUtil.formatToDate(time) :  new Date();
	    String timeDate = DateFormatUtil.dateToString(timeDt);
	    
	    requestLog.setTime(timeDt);
	    whereParams.put("tableSeq", requestLog.getTableSeq());
	    
	    if (StringUtil.isNotBlank(start_time)) {
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
	    	start_time = sdfHour.format(new Date()) +  ":00";
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    }
	    
	    if (StringUtil.isNotBlank(end_time)) {
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
	    	end_time = sdfTime.format(new Date());
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    }
	    
	    if (verify_code != null) {
	    	whereParams.put("verify_code", verify_code);
	    }
	    
	    List<RequestLog> dataList = requestLogService.statByCode(whereParams);
	    
	    model.addAttribute("time", time);
	    model.addAttribute("start_time", start_time);
	    model.addAttribute("end_time", end_time);
	    model.addAttribute("verify_code", verify_code);
		model.addAttribute("dataList", dataList);
		return "bs/requestlogstat/statByCode";
	}

}
